<?php

namespace app\admin\service;

use app\admin\model\BoxMemberChangeLog;
use app\admin\model\BoxNotice;
use app\admin\model\Member;
use app\common\controller\Notice;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;

class BoxMemberChangeLogService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxMemberChangeLog();
    }

    /**
     * 查看审核中的头像
     * @return array
     */
    public function getAvatarList(): array
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        $map[] = ['type_name', '=', 'avatar'];

        if (isset($param['status']) && $param['status']!='') {
            $map[] = ['status', '=', $param['status'] ?? 0];
        }
        $data = parent::getListNew($map);
        if ($data['count'] <= 0) return $data;
        foreach ($data['data'] as $key => $item) {
            $data['data'][$key]['old_value'] = imageUrl($item['old_value']);
            $data['data'][$key]['new_value'] = imageUrl($item['new_value']);
        }
        return $data;// TODO: Change the autogenerated stub
    }

    /**
     * 头像审核状态
     * @return array
     * @throws BindParamException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public function avatarStatus(): array
    {

        $param = request()->param();
        if (!$param['id']) {
            return message('记录ID不能为空', false);
        }
        if (!$param['status']) {
            return message('记录状态不能为空', false);
        }
        $param = request()->param();
        if ($param['status'] == -1 && empty($param['reason'])) {
            return message('请输入拒绝原因', false);
        }

        $item = [
            'id' => $param['id'],
            'status' => $param['status'],
            'examine_time' => time()
        ];

        $info = $this->model->getInfo($param['id']);
        if ($param['status'] == 1) {//更新用户头像
            Member::where('id', $info['member_id'])->update(['avatar' => $info['new_value'], 'update_time' => time()]);
            $content = '尊敬的用户您好，您上传的头像已经通过平台审核，快去看看吧！';
            $source = $this->model::AVATAR_REVIEW_SUCCESS;
        } else {
//            $content = '尊敬的用户您好，由于您上传的头像' . $param['reason'] . ',已经被平台删除~';
            $content = $param['reason'];
            $item['reason'] = $param['reason'];
            $source = $this->model::AVATAR_REVIEW_ERROR;
        }
        $error = '';
        $rowId = $this->model->edit($item, $error);
        if ($rowId === false) {
            return message($error, false);
        }
        (new  Notice($info['member_id']))->sendToUser($source,$content,$rowId);
        //(new BoxNoticeService())->sendNotice($info['member_id'], '头像审核结果通知', $content,$source);
        return message();

    }
}